package com.yantong.quartz.job;

import com.yantong.pojo.Member;
import com.yantong.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * vip相关定时任务
 *
 * @author liaozhaogui-773104632@qq.com
 * @date 2021/1/27 9:52
 */
@Service
public class vipTimer {
	private static final Logger logger = LoggerFactory.getLogger(vipTimer.class);

	    private boolean taskOn = true;
	@Autowired
	private MemberService memberService;


	/**
	 * 每天凌晨会员过期处理
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	//@Scheduled(cron = "0 28 10 * * ?")
	public void assetReleaseFlow() {
		if (!taskOn) {
			logger.info("system task set off!autoDelivery!");
			return;
		}
		//查询所有vip会员
		Map<String, Object> searchMap = new HashMap<>(16);
		searchMap.put("isVip", 1);
		List<Member> list = memberService.getAllMember(searchMap);
		for (Member member : list) {
			//判断会员是否过期
			if (member.getVipEndTime() != null && member.getVipTime() != null && new Date().compareTo(member.getVipEndTime()) > 0) {
				member.setIsVip(0);
				//过期修改会员状态
				memberService.updateMemberAsync(member);
			}
		}
	}
}
